package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.hqyj.pojo.Menber;
import com.hqyj.pojo.Reservation;
import com.hqyj.dao.ReservationMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;

/**
 * <p>
 * 实时消费表 服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-21
 */
@Service
public class ReservationServiceImpl  implements ReservationService {
    //注入mapper
    @Autowired
    ReservationMapper reservationMapper;

    @Override
    public HashMap<String, Object> select(Reservation reservation) {
        HashMap<String,Object> map = new HashMap<>();
        //定义分页对象
        Page<Reservation> page = new Page<>(reservation.getPage(),reservation.getRow());
        //条件构造器
        QueryWrapper<Reservation> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(reservation.getMenberName())&&reservation.getMenberName()!=null,"menber_name",reservation.getMenberName());

        //查询数据库
        Page<Reservation> reList = reservationMapper.selectPage(page, queryWrapper);
        //当前页集合
        map.put("list",reList.getRecords());
        //总页数
        map.put("totalPage",reList.getPages());
        //总条数
        map.put("total",reList.getTotal());
        //当前页
        map.put("curPage",reservation.getPage());
        if(reservation.getPage()==1){
            //上一页
            map.put("prePage",1);
        }else{
            map.put("prePage",reservation.getPage()-1);
        }
        if(reservation.getPage()==reList.getPages()){
            //下一页
            map.put("nextPage",reList.getPages());
        }else{
            map.put("nextPage",reservation.getPage()+1);
        }
        //每页显示条数
        map.put("row",reservation.getRow());
        return map;
    }

    @Override
    public HashMap<String, Object> update(Reservation reservation) {
        HashMap<String, Object> map = new HashMap<>();
        int num = reservationMapper.updateById(reservation);
        if (num > 0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> add(Reservation reservation) {
        HashMap<String, Object> map = new HashMap<>();
        int num = reservationMapper.insert(reservation);
        if (num > 0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        int num = reservationMapper.deleteById(id);
        if (num > 0){
            map.put("info","删除成功");
        }else {
            map.put("info","删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> selectByName(Reservation reservation) {
        HashMap<String, Object> map = new HashMap<>();
        int num = reservationMapper.selectByName(reservation);
        if (num > 0){
            map.put("info","保存成功");
        }else {
            map.put("info","保存失败");
        }
        return map;
    }
}
